using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.Controls;
using WP.Modules;
using WP.DB;
using WP.MF.DataAccess;

namespace WP.MF.Form
{
    public partial class frmMProcessing : BaseQryRpt
    {

        #region ***Define Variable
        private MenuInfo m_MenuInfo = new MenuInfo();
        public override MenuInfo CurrentMenuInfo
        {
            get
            {
                return m_MenuInfo;
            }
            set
            {
                m_MenuInfo = value;
                InitControl();
            }
        }
        #endregion Define Variable***

        #region ***Constructor Functions
        public frmMProcessing()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
        }
        public void ShowMe()
        {
            InitControl();
            this.Show();
        }
        #endregion Constructor Functions***

        #region ***Form Events
        private bool mblnBackUpFinish = false;
        private void frmImProcessing_Load(object sender, System.EventArgs e)
        {
        }

       

        private void btnOk_Click(object sender, EventArgs e)
        {
            bool bResult = false;
            WP.MF.DataAccess.MProcessingDAL imProDAL = new MProcessingDAL(); 

            this.Cursor = Cursors.WaitCursor;
                        
            DBAccess dbcon = new DBAccess(false, false);
            
            prgBar.Visible = false;
            switch (this.CurrentMenuInfo.iParameter)
            {
                case 1:  //MONTH-END PROCESSING            

                    string strSql = "Delete from mUserDefault where userName='" + "" + "' and ProgName='" + this.Name + "' and  Parameter='0'";
                    dbcon.ExecuteSql(strSql);

                    //strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                    //    "" + "','" + this.Name + "','" + "0" + "','" + txtSlowMovingMonths.Name + "','" + (txtSlowMovingMonths.Text) + "')";
                    //dbcon.ExecuteSql(strSql);
                    //strSql = "Insert into mUserDefault (UserName, ProgName,Parameter,Name,DefaultString) values ('" +
                    //    "" + "','" + this.Name + "','" + "0" + "','" + txtSlowMovingPct.Name + "','" + (txtSlowMovingPct.Text) + "')";
                    //dbcon.ExecuteSql(strSql);


                    bResult = imProDAL.MonthEnd(this.prgBar, this.txtCurPrd.Text, this.txtNewPrd.Text, false, false, CvtFunc.ToInt(txtSlowMovingMonths.Text), CvtFunc.ToDouble(txtSlowMovingPct.Text));
                    break;
                case 2:  //YEAR-END PROCESSING
                    bResult = imProDAL.YearEnd(this.prgBar, this.txtInvHis.Text, this.txtNewDate.Text);
                    break;
                default:
                    break;
            }
            this.Cursor = Cursors.Default;

            if (bResult) this.Close();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void Field_Leave(object sender, System.EventArgs e)
        {
            Control con = sender as Control;
            if (con == null) return;

            ////if (con.Name == txtReHis.Name ||
            ////    con.Name == txtSaHis.Name)
            ////{
            ////    con.Text = CvtFunc.ToInt(con.Text).ToString();
            ////    if (CvtFunc.ToInt(con.Text) <= 0)
            ////    {
            ////        MsgBoxEx.Show("Years must be greater than zero!", SysVar.WarnTitle);
            ////        con.Focus();
            ////    }
            ////}
        }

        #endregion Form Events***

        #region ***Form Functions
        private void InitControl()
        {
            switch (this.CurrentMenuInfo.iParameter)
            {
                case 1:
                    if (CvtFunc.ToInt(DataFunctions.GetPrdInfo( SystemModules.IM, PeriodRange.PeriodCur, PeriodOption.Period)) == 13)
                    {
                        MsgBoxEx.Show(Msg.Select("The previous year must be closed before this month can be closed.", DataFunctions.GetMsg(this.Name, "MSG_PreviousYearMustBeClosed")), SysVar.WarnTitle);
                        this.Close();
                        return;
                    }
                    this.Text =Msg.Select("MONTH-END PROCESSING", DataFunctions.GetMsg(this.Name, "MSG_MonthEndProcessing"));
                    gpbMonth.Visible = true;
                    this.groupBox1.Visible = false;
                    //if (DataFunctions.GetmSwitchValue("UseSlowMovingItem", "IM"))
                    //{
                    //    this.groupBox1.Visible = true;
                    //    int iMonth = CvtFunc.ToInt(DataFunctions.GetFieldValue("mUserDefault", "DefaultString", "ProgName='" + this.Name + "' and Name='" + txtSlowMovingMonths.Name +"'"));
                    //    txtSlowMovingMonths.Text =(iMonth>0 ? iMonth.ToString() : "6");
                    //    double dblPct = CvtFunc.ToDouble(DataFunctions.GetFieldValue("mUserDefault", "DefaultString", "ProgName='" + this.Name + "' and Name='" + txtSlowMovingPct.Name + "'"));
                    //    txtSlowMovingPct.Text = (dblPct > 0 ? dblPct.ToString() : "1");
                    //}
                    //lblDesc0.Text = "Press OK to record the current inventory value.";
                    lblDesc1.Text = "";
                    txtCurPrd.Text = CvtFunc.ToString(CvtFunc.ToInt(DataFunctions.GetPrdInfo( SystemModules.RM, PeriodRange.PeriodCur, PeriodOption.Period)));
                    txtCurSt.Text = BaseFunctions.FormatDate(DataFunctions.GetPrdInfo(SystemModules.RM, PeriodRange.PeriodCur, PeriodOption.StPrdDate));
                    txtCurEd.Text = BaseFunctions.FormatDate(DataFunctions.GetPrdInfo(SystemModules.RM, PeriodRange.PeriodCur, PeriodOption.EnPrdDate));
                    txtNewPrd.Text = CvtFunc.ToString(CvtFunc.ToInt(txtCurPrd.Text) + 1);
                    txtNewSt.Text = BaseFunctions.FormatDate(DataFunctions.GetPrdInfo(SystemModules.RM,(PeriodRange)CvtFunc.ToInt(txtNewPrd.Text), PeriodOption.StPrdDate));
                    txtNewEd.Text = BaseFunctions.FormatDate(DataFunctions.GetPrdInfo(SystemModules.RM, (PeriodRange)CvtFunc.ToInt(txtNewPrd.Text), PeriodOption.EnPrdDate));
                    break;
                case 2:
                    this.groupBox1.Visible = false;
                    if (CvtFunc.ToInt(DataFunctions.GetPrdInfo(SystemModules.RM, PeriodRange.PeriodCur, PeriodOption.Period)) != 13)
                    {
                        MsgBoxEx.Show(Msg.Select("Cannot close year till last month has been closed.", DataFunctions.GetMsg(this.Name, "MSG_CanNotCloseYear")), SysVar.WarnTitle);
                        this.Close();
                        return;
                    }
                    this.Text =Msg.Select("YEAR-END PROCESSING", DataFunctions.GetMsg(this.Name, "MSG_YearEndProcessing"));
                    gpbYear.Visible = true;
                    txtNewDate.Text = BaseFunctions.FormatDate(DataFunctions.GetPrdInfo( SystemModules.RM, PeriodRange.Period13, PeriodOption.StPrdDate));
                    //txtInvHis.Text = DataFunctions.GetmMiscValue("SaveImHisNoOfYr", "im").TrimEnd();
                    break;
                default:
                    break;
            }
        }
        #endregion Form Functions***

        

    }
}